#include "main.h"
#pragma anon_unions

typedef union {
	u16  Cell_BL_Value;                /*BMS运行状态值*/
    struct {
		u8 Cell_BL1 	:1;
		u8 Cell_BL2		:1;
		u8 Cell_BL3		:1;
		u8 Cell_BL4		:1;
		u8 Cell_BL5		:1;
		u8 Cell_BL6		:1;
		u8 Cell_BL7		:1;
		u8 Cell_BL8		:1;
		u8 Cell_BL9		:1;
		u8 Cell_BL10	:1;
		u8 Cell_BL11	:1;
		u8 Cell_BL12	:1;
		u8 Cell_BL13	:1;
		u8 Cell_BL14	:1;
		u8 Cell_BL15	:1;
		u8 Cell_BL16	:1;
    };
} APP_BL_STS;

APP_BL_STS G_Appl_Bl_Sts;


u16  G_u16_Blance_Cmd;


extern u16	APPL_VB_U;
u16 G_u16_MC3371_AdVlaue[30];
float BSW_BattBalanTmp;
float BSW_BattIcTemp;
float BSW_BattFuseVolt;
float BSW_BattRelayVolt;
float BSW_BattPackTempNr1;
float BSW_BattPackTempNr2;
float BSW_BattPackVolt;
float BSW_BattPackCurr;


MC33771_CMD_FLG  MC33771_BLANCE[14] =
{
		{	0x02,	0xFF,	CB1_CFG,	0x12,	0x57	},	/*1	*/
		{	0x02,	0xFF,	CB2_CFG,	0x12,	0xBE	},	/*2	*/
		{	0x02,	0xFF,	CB3_CFG,	0x12,	0xAA	},	/*3	*/
		{	0x02,	0xFF,	CB4_CFG,	0x12,	0x43	},	/*4	*/
		{	0x02,	0xFF,	CB6_CFG,	0x12,	0xCB	},	/*5	*/
		{	0x02,	0xFF,	CB7_CFG,	0x12,	0xDF	},	/*6	*/
		{	0x02,	0xFF,	CB8_CFG,	0x12,	0x36	},	/*7	*/
		{	0x02,	0xFF,	CB9_CFG,	0x12,	0xF7	},	/*8	*/
		{	0x02,	0xFF,	CB10_CFG,	0x12,	0x1E	},	/*9	*/
		{	0x02,	0xFF,	CB11_CFG,	0x12,	0x0A	},	/*10	*/
		{	0x02,	0xFF,	CB12_CFG,	0x12,	0xE3	},	/*11	*/
		{	0x02,	0xFF,	CB13_CFG,	0x12,	0xA7	},	/*12	*/
		{	0x02,	0xFF,	CB14_CFG,	0x12,	0x4E	},	/*13	*/
		{	0x00,	0x00,	0x00,	0x00,	0xB2	},	/*14	*/
};



void APPL_MC33771_BLANCE_Contrl(u16 u16_CellFlg)
{
	u8* p_u8_Data;
	u8 u8_Count = 0;
	for(u8_Count = 0;u8_Count <14;u8_Count++)
	{
		MC33771_BLANCE[u8_Count].u8_DataL =((u16_CellFlg & 0x01)<<1);
		u16_CellFlg = u16_CellFlg >> 1;
	}
	for(u8_Count = 0;u8_Count <14;u8_Count++)
	{
		p_u8_Data = (u8*)(&MC33771_BLANCE[u8_Count]);
		*(p_u8_Data+4) = MC33771_CRC8(p_u8_Data, 4);
	}
	ECU_MC33771_Blance((u32)MC33771_BLANCE,14);
}






void APPL_MC33771_Cell_VCT(void);


inline void APPL_MC33771_Clear_FLT(void)
{
	ECU_MC33771_Clear_Flt();
}




/*********运行MC33771功能获取有效信息********/
inline void APPL_MC33771_RUN(void)
{
	APPL_MC33771_Cell_VCT();
}



/*标定的四个参数*/
float G_f_Curr_K1 = 0.95;
float G_f_Curr_K2 = 0.01;
float G_f_Curr_K3 = 0.04;
float G_f_Curr_K4 = 0.5;


float APPL_MC33771_Curr(float f_Real_Curr,float f_Appl_Curr)
{
	float f_Curr_Data = 0;
    if(BSW_BattBalanTmp > 100)
    {
       BSW_BattBalanTmp =100;
    }
    else if(BSW_BattBalanTmp <-50)
    {
      BSW_BattBalanTmp =-50;
    }
    if(G_f_Curr_K1 < 0.8)
    {
    	G_f_Curr_K1 = 0.8;
    }
    else if(G_f_Curr_K1 > 1.2)
    {
    	G_f_Curr_K1 = 1.2;
    }
    f_Real_Curr = f_Real_Curr / (G_f_Curr_K1 *(1 + (BSW_BattBalanTmp* BSW_BattBalanTmp* -(G_f_Curr_K2/1000)+ (BSW_BattBalanTmp*G_f_Curr_K3) - G_f_Curr_K4)));

    f_Curr_Data = f_Appl_Curr *0.9 + f_Real_Curr * 0.1;
    return f_Curr_Data;
}





float	G_f_BattPackTemp1;
float	G_f_BattPackTemp2;
float	G_f_BattBalanTmp;
float	G_f_BattPackCurr;

float	G_f_BattFuseVolt;
float	G_f_BattRelayVolt;
float	G_f_BattPackVolt;


float APPL_MC33771_Data_Filter(float Real_Data,float Appl_Data,float f_P_Data)
{
	float f_Data_Value = 0;
	if(Appl_Data != 0)
	{
		f_Data_Value = Real_Data - Appl_Data;
		f_Data_Value = Appl_Data + f_Data_Value * f_P_Data;
	}
	else
	{
		f_Data_Value = Real_Data;
	}
	return f_Data_Value;
}


float G_f_Clb_Cur = 0;
s32 G_s32_Cl_Value = 0;
u16 G_u16_Clb_Count = 0;

	float f_CellU[14] = {0};
volatile u8 G_u8_Cell_Count = 0;
volatile u8 G_u8_Vct_Step = 0;
/************************************************
**函数名：	APPL_MC33771_Cell_VCT
**功能：	读取MC33771的电压 电流 温度存放到应用层指定的变量中
**输入：	无
**输出：	无
**注意：	注意调用位置 测量其运行时间
************************************************/
void APPL_MC33771_Cell_VCT(void)
{
	
	ECU_MC33771_CCELL_VCT(G_u16_MC3371_AdVlaue);
//	f_CellU[0] = 	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[1]);
//	f_CellU[1] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[2]);
//	f_CellU[2] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[3]);
//	f_CellU[3] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[4]);
//	f_CellU[4] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[5]);
//	f_CellU[5] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[6]);
//	f_CellU[6] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[7]);
//	f_CellU[7] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[8]);
//	f_CellU[8] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[9]);
//	f_CellU[9] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[10]);
//	f_CellU[10] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[11]);
//	f_CellU[11] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[12]);
//	f_CellU[12] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[13]);

	/*以下代码执行时间16us*/
	G_f_BattFuseVolt 		= 	ECU_MC33771_ClltFuseVol(G_u16_MC3371_AdVlaue[14]);
	G_f_BattRelayVolt 		=	ECU_MC33771_ClltRelayVol(G_u16_MC3371_AdVlaue[15]);
	G_f_BattPackTemp1 		=	ECU_MC33771_ClltTemp(G_u16_MC3371_AdVlaue[16]);
	G_f_BattBalanTmp 		= 	ECU_MC33771_BlanceTemp(G_u16_MC3371_AdVlaue[17]);
	G_f_BattPackTemp2		=	ECU_MC33771_ClltTemp(G_u16_MC3371_AdVlaue[18]);

//	G_f_BattPackCurr 		= 	ECU_MC33771_ClltCurr(G_u16_MC3371_AdVlaue[21],G_u16_MC3371_AdVlaue[22]);
	G_f_BattPackVolt		=  	ECU_MC33771_ClltPackVol(G_u16_MC3371_AdVlaue[23]);
	BSW_BattIcTemp			= 	ECU_MC33771_IC_Temp(G_u16_MC3371_AdVlaue[24]);
	
	
	Diag_Cell_BAL_SW_Flg |= G_u16_MC3371_AdVlaue[29] & 0x10;
	

	if((G_u16_MC3371_AdVlaue[29] & 0xC000) > 0)
	{
		Diag_SW5V_FLT_Flg |= 1;
	}
	else
	{
		Diag_SW5V_FLT_Flg = 0;
	}
	
	
	
	
	
	BSW_BattFuseVolt =	APPL_MC33771_Data_Filter(G_f_BattFuseVolt,	BSW_BattFuseVolt,	0.1);
	BSW_BattRelayVolt =	APPL_MC33771_Data_Filter(G_f_BattRelayVolt,	BSW_BattRelayVolt,	0.1);
	BSW_BattPackTempNr1 = APPL_MC33771_Data_Filter(G_f_BattPackTemp1,	BSW_BattPackTempNr1,	0.1);
	BSW_BattPackTempNr2 = APPL_MC33771_Data_Filter(G_f_BattPackTemp2,	BSW_BattPackTempNr2,	0.1);
	BSW_BattBalanTmp = APPL_MC33771_Data_Filter(G_f_BattBalanTmp,	BSW_BattBalanTmp,	0.1);
	BSW_BattPackVolt = APPL_MC33771_Data_Filter(G_f_BattPackVolt,	BSW_BattPackVolt,	0.1);

	APPL_VB_U = (u16)BSW_BattFuseVolt;

	G_s32_Cl_Value	= 	G_u16_MC3371_AdVlaue[25];
	G_s32_Cl_Value 	= 	(G_s32_Cl_Value<<16) + G_u16_MC3371_AdVlaue[26];
	G_u16_Clb_Count	= 	G_u16_MC3371_AdVlaue[27];
	G_f_Clb_Cur = G_s32_Cl_Value;
	if(G_u16_Clb_Count != 0)
	{
		G_f_Clb_Cur = (G_f_Clb_Cur/ G_u16_Clb_Count)* 6/1000;
	}
	BSW_BattPackCurr = APPL_MC33771_Curr(G_f_Clb_Cur,BSW_BattPackCurr);

	switch (G_u8_Vct_Step)
	{
		case 0:
		{
//			BSW_CellU01=	APPL_MC33771_Data_Filter(f_CellU[0],	BSW_CellU01,	0.05);
//			BSW_CellU02=	APPL_MC33771_Data_Filter(f_CellU[1],	BSW_CellU02,	0.05);
//			BSW_CellU03=	APPL_MC33771_Data_Filter(f_CellU[2],	BSW_CellU03,	0.05);
//			BSW_CellU04=	APPL_MC33771_Data_Filter(f_CellU[3],	BSW_CellU04,	0.05);
//			BSW_CellU05=	APPL_MC33771_Data_Filter(f_CellU[4],	BSW_CellU05,	0.05);
//			BSW_CellU06=	APPL_MC33771_Data_Filter(f_CellU[5],	BSW_CellU06,	0.05);
//			BSW_CellU07=	APPL_MC33771_Data_Filter(f_CellU[6],	BSW_CellU07,	0.05);
//			BSW_CellU08=	APPL_MC33771_Data_Filter(f_CellU[7],	BSW_CellU08,	0.05);
//			BSW_CellU09=	APPL_MC33771_Data_Filter(f_CellU[8],	BSW_CellU09,	0.05);
//			BSW_CellU10=	APPL_MC33771_Data_Filter(f_CellU[9],	BSW_CellU10,	0.05);
//			BSW_CellU11=	APPL_MC33771_Data_Filter(f_CellU[10],	BSW_CellU11,	0.05);
//			BSW_CellU12=	APPL_MC33771_Data_Filter(f_CellU[11],	BSW_CellU12,	0.05);
//			BSW_CellU13=	APPL_MC33771_Data_Filter(f_CellU[12],	BSW_CellU13,	0.05);
			
		f_CellU[0] = 	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[1]);
		f_CellU[1] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[2]);
		f_CellU[2] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[3]);
		f_CellU[3] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[4]);
		f_CellU[4] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[5]);
		f_CellU[5] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[6]);
		f_CellU[6] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[7]);
		f_CellU[7] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[8]);
		f_CellU[8] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[9]);
		f_CellU[9] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[10]);
		f_CellU[10] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[11]);
		f_CellU[11] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[12]);
		f_CellU[12] =	ECU_MC33771_ClltCellVol(G_u16_MC3371_AdVlaue[13]);

			if(G_Appl_Bl_Sts.Cell_BL_Value)
			{
				/*发送开启均衡的指令*/
				APPL_MC33771_BLANCE_Contrl(G_Appl_Bl_Sts.Cell_BL_Value);
				G_u8_Vct_Step = 1;
			}
			else
			{
				ECU_MC33771_GVal_SttCov();
			}
			break;
		}
		case 1:
		{
			if(G_u8_Cell_Count > 4)
			{
				/*发送关闭均衡的指令*/
				APPL_MC33771_BLANCE_Contrl(0);
				G_u8_Cell_Count = 0;
				G_u8_Vct_Step = 2;
			}
			else
			{
				G_u8_Cell_Count++;
				ECU_MC33771_GVal_SttCov();
			}
			break;
		}
		case 2:
		{
			if(G_u8_Cell_Count > 1)
			{
				G_u8_Cell_Count = 0;
				G_u8_Vct_Step = 0;
			}
			else
			{
				G_u8_Cell_Count ++;
			}
			ECU_MC33771_GVal_SttCov();
			break;
		}
		default:
		{
			G_u8_Vct_Step = 0;
			break;
		}
	}
}




const MC33771_CMD_FLG  AFE_CELL_DIAG[60] =
{
	/*	进入诊断前清除所有诊断故障 - 检测单体开路故障		*/
	{	0x00,	0x00,	CELL_OV_FLT,	0x12,	0x2E,	},	/*01	*/
	{	0x00,	0x00,	CELL_UV_FLT,	0x12,	0x3A,	},	/*02	*/
	{	0x13,	0xC6,	SYS_CFG1,	0x12,	0xC8	},	/*03	*/
	{	0xDC,	0x66,	TH_ALL_CT,	0x12,	0x8C	},	/*04	*/
	{	0xFF,	0xFF,	OV_UV_EN,	0x12,	0x43	},	/*05	*/
	{	0x00,	0x08,	SYS_DIAG,	0x12,	0x0E 	},	/*06	*/
	{	0x0C,	0xBF,	ADC_CFG,	0x12,	0x43	},	/*07	*/
	{	0x13,	0x86,	SYS_CFG1,	0x12,	0x39	},	/*08	*/

	/************************40*******************/
	{	0x00,	0x01,	CELL_OV_FLT,	0x11,	0x51	},	/*1	*/
	{	0x00,	0x01,	CELL_UV_FLT,	0x11,	0x45	},	/*2	*/
	{	0x13,	0x86,	SYS_CFG1,	0x12,	0x39	},	/*3	*/
	/**********************55*********************/
	{	0x00,	0x00,	CELL_OV_FLT,	0x12,	0x2E,	},	/*4	*/
	{	0x00,	0x00,	CELL_UV_FLT,	0x12,	0x3A,	},	/*5	*/
	{	0x0C,	0xBF,	ADC_CFG,	0x12,	0x43	},	/*6	*/

	
	/*	进入诊断前清除所有诊断故障 - 检测分流器开路故障 均衡回路故障*/
	/**********************70*********************/
	{	0x13,	0xE6,	SYS_CFG1,	0x12,	0x27	},	/*01	*/
	{	0x04,	0x01,	SYS_DIAG,	0x12,	0xFA	},	/*02	*/
	{	0x00,	0x00,	OV_UV_EN,	0x12,	0x43	},	/*03	*/
	{	0x00,	0x00,	CB_OPEN_FLT,	0x12,	0x1F	},	/*04	*/
	{	0x00,	0x00,	CB_SHORT_FLT,	0x12,	0xF6	},	/*05	*/
	{	0x00,	0x00,	FAULT1_STATUS,	0x12,	0xDD	},	/*06	*/
	{	0x00,	0x00,	FAULT2_STATUS,	0x12,	0x34	},	/*07	*/
	{	0x00,	0x00,	FAULT3_STATUS,	0x12,	0x20	},	/*08	*/
	{	0x0C,	0xBF,	ADC_CFG,	0x12,	0x43	},	/*09	*/

	/**********************115*********************/
	{	0x00,	0x01,	CB_OPEN_FLT,	0x11,	0x60	},	/*01	*/
	{	0x00,	0x01,	CB_SHORT_FLT,	0x11,	0x89	},	/*02	*/
	{	0x00,	0x01,	FAULT1_STATUS,	0x11,	0xA2,	},	/*03	*/
	{	0x00,	0x01,	FAULT2_STATUS,	0x11,	0x4B,	},	/*04	*/
	{	0x00,	0x01,	FAULT3_STATUS,	0x11,	0x5F,	},	/*05	*/
	{	0x04,	0x02,	SYS_DIAG,	0x12,	0xE8	},	/*06	*/
	/*********************145*********************/
	{	0x00,	0x00,	CB_OPEN_FLT,	0x12,	0x1F	},	/*07	*/
	{	0x00,	0x00,	CB_SHORT_FLT,	0x12,	0xF6	},	/*08	*/
	{	0x00,	0x00,	FAULT1_STATUS,	0x12,	0xDD	},	/*09	*/
	{	0x00,	0x00,	FAULT2_STATUS,	0x12,	0x34	},	/*10	*/
	{	0x00,	0x00,	FAULT3_STATUS,	0x12,	0x20	},	/*11	*/
	{	0x0C,	0xBF,	ADC_CFG,	0x12,	0x43	},	/*12	*/
	
	/**********************175*********************/
	{	0x13,	0x86,	SYS_CFG1,	0x12,	0x39	},	/*01	*/
	{	0x00,	0x00,	CELL_OV_FLT,	0x12,	0x2E,	},	/*02	*/
	{	0x00,	0x00,	CELL_UV_FLT,	0x12,	0x3A,	},	/*03	*/
	{	0x00,	0x00,	CB_OPEN_FLT,	0x12,	0x1F	},	/*04	*/
	{	0x00,	0x00,	CB_SHORT_FLT,	0x12,	0xF6	},	/*05	*/
	{	0x00,	0x00,	FAULT1_STATUS,	0x12,	0xDD	},	/*06	*/
	{	0x00,	0x00,	FAULT2_STATUS,	0x12,	0x34	},	/*07	*/
	{	0x0C,	0xBF,	ADC_CFG,	0x12,	0x43	},	/*08	*/

};




u8 APPL_MC33771_DIAG_CT(void)
{
	u8 u8_status = false;
	static u8 S_u8_DiagCt = 0;
	static u8 S_u8_DCt_Dly = 0;
	static u8 S_u8_DCt_ErrCnt = 0;
	switch(S_u8_DiagCt)
	{
			case 0:			/*进入诊断模式*/
			{
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG,7);
				S_u8_DiagCt = 1;
				S_u8_DCt_Dly = 0;
				break;
			}
			case 1:			
			{
				if(S_u8_DCt_Dly > 2)/*大小与周期稳定性有关*/
				{
					S_u8_DiagCt = 2;
					S_u8_DCt_Dly = 0;
				}
				else
				{
					S_u8_DCt_Dly ++;
					ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 55,2); 
				}
				break;
			}
			case 2:			
			{
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 40,5);
				S_u8_DiagCt = 3;
				break;
			}
			case 3:			
			{
				ECU_MC33771_CCELL_VCT(G_u16_MC3371_AdVlaue);
				if(G_u16_MC3371_AdVlaue[1] != 0x00 || G_u16_MC3371_AdVlaue[2] != 0x0010)
				{
					if(S_u8_DCt_ErrCnt > 2)
					{
						u8_status = true;
						/*故障成立*/
						Diag_CellSamplingOpen_Flg = true;
						S_u8_DCt_ErrCnt = 0;
					}
					else
					{
						S_u8_DCt_ErrCnt ++;
					}
				}
				else
				{
					Diag_CellSamplingOpen_Flg = false;
					u8_status = true;
				}
        S_u8_DiagCt = 0;
				break;
				
			}
			default:
			{
				S_u8_DiagCt = 0;
				break;
			}
	}
	return u8_status;
}

u8 APPL_MC33771_DIAG_CB_IS(void)
{
	u8 u8_status = false;
	static u8 S_u8_DCbIs = 0;
	
	switch(S_u8_DCbIs)
	{
			case 0:			/*进入诊断模式*/
			{
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 70,8);
				S_u8_DCbIs = 1;
				break;
			}
			case 1:			
			{
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 115,11);
				S_u8_DCbIs = 2;
				break;
			}
			case 2:
			{
				ECU_MC33771_CCELL_VCT(G_u16_MC3371_AdVlaue);
				if(G_u16_MC3371_AdVlaue[1] != 0)
				{
					Diag_Cell_BAL_SW_Flg |= 1;
				}
				else
				{
					Diag_Cell_BAL_SW_Flg = 0;
				}
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 115,11);
				S_u8_DCbIs = 3;
				break;
			}
			case 3:
			{
				ECU_MC33771_CCELL_VCT(G_u16_MC3371_AdVlaue);
				if(G_u16_MC3371_AdVlaue[1] != 0)
				{
					Diag_Cell_BAL_SW_Flg |= 1;
				}
				if((G_u16_MC3371_AdVlaue[3] & 0x20) == 0x20)
				{
					Diag_CurrSamplingOpen_Flg |= 1;
				}
				else
				{
					Diag_CurrSamplingOpen_Flg = 0;
				}
				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 175,7);
				u8_status = true;
				S_u8_DCbIs = 0;
				break;
			}
			default:
			{
				S_u8_DCbIs = 0;
				break;
			}
	}
	return u8_status;
}










u8 APPL_MC33771_DIAG(void)
{
	static u8 s_u8_BLN_Stp = 0;
	u8 u8_status = 0;
	switch(s_u8_BLN_Stp)
		{
			case 0:			/*进入诊断模式*/
			{
				u8_status = APPL_MC33771_DIAG_CT();
				if(u8_status)
				{
					s_u8_BLN_Stp = 1;
					u8_status = 0;
				}
				break;
			}
			case 1:			
			{
				u8_status = APPL_MC33771_DIAG_CB_IS();
				if(u8_status)
				{
					s_u8_BLN_Stp = 2;
					u8_status = 0;
				}
				break;
			}
			case 2:			
			{
				ECU_MC33771_Normal();
				s_u8_BLN_Stp = 3;
				break;
			}
			case 3:
			{
				ECU_MC33771_GVal_SttCov();
				s_u8_BLN_Stp = 0;
				u8_status = true;
				break;
			}
			default:
			{
				s_u8_BLN_Stp = 0;
				break;
			}
	}
	return u8_status;
}



























void APPL_MC33771_BLANCE(u16 u16_CellFlg)
{
	//	static u8 S_u8_Blance_Flg = 0;
//	u8* p_u8_Data;
//	u8 u8_Count = 0;
//	G_Appl_Bl_Sts.Cell_BL_Value = u16_CellFlg;

//	if(u16_CellFlg)
//	{
//		S_u8_Blance_Flg = 0;
//		for(u8_Count = 0;u8_Count <14;u8_Count++)
//		{
//			MC33771_BLANCE[u8_Count].u8_DataL =((u16_CellFlg & 0x01)<<1);
//			u16_CellFlg = u16_CellFlg >> 1;
//		}
//		for(u8_Count = 0;u8_Count <14;u8_Count++)
//		{
//			p_u8_Data = (u8*)(&MC33771_BLANCE[u8_Count]);
//			*(p_u8_Data+4) = MC33771_CRC8(p_u8_Data, 4);
//		}
//		ECU_MC33771_Blance((u32)MC33771_BLANCE,14);
//	}
//	else
//	{
//		if(S_u8_Blance_Flg == 0)
//		{
//			for(u8_Count = 0;u8_Count <14;u8_Count++)
//			{
//				MC33771_BLANCE[u8_Count].u8_DataL =((u16_CellFlg & 0x01)<<1);
//				u16_CellFlg = u16_CellFlg >> 1;
//			}
//			for(u8_Count = 0;u8_Count <14;u8_Count++)
//			{
//				p_u8_Data = (u8*)(&MC33771_BLANCE[u8_Count]);
//				*(p_u8_Data+4) = MC33771_CRC8(p_u8_Data, 4);
//			}
//			ECU_MC33771_Blance((u32)MC33771_BLANCE,14);
//			S_u8_Blance_Flg = 1;
//		}
//	}
}


//			case 0:			/*进入诊断模式*/
//			{
//				ECU_MC33771_CMD((u32)AFE_CELL_DIAG,10);
//				s_u8_BLN_Stp = 1;
//				break;
//			}
//			case 1:			/*读取奇数单体的开路状态*/
//			{
//				ECU_MC33771_CMD((u32)AFE_CHANGE_CELL,7);
//				s_u8_BLN_Stp = 2;
//				break;
//			}
//			case 2:
//			{
//				ECU_MC33771_CMD((u32)AFE_CHANGE_CELL,7);
//				s_u8_BLN_Stp = 3;
//				break;
//			}
//			case 3:
//			{
//				
//				ECU_MC33771_CMD((u32)AFE_DIAG_Read,24);
//				s_u8_BLN_Stp = 4;
//				break;
//			}
//			case 4:
//			{
//				APPL_AFE_DIAG_CellOvUv();
//				G_u16_Cell_UvOv1[0] = G_u16_MC3371_AdVlaue[15];
//				G_u16_Cell_UvOv1[1] = G_u16_MC3371_AdVlaue[16];
//				G_u16_Cell_UvOv2[0] = G_u16_MC3371_AdVlaue[20];
//				G_u16_Cell_UvOv2[1] = G_u16_MC3371_AdVlaue[21];
//				ECU_MC33771_GVal_SttCov();
//				
//				s_u8_BLN_Stp = 0;
//				u8_status = true;
//				
//				s_u8_BLN_Stp = 5;
//				break;
//			}



//			case 0:			/*进入诊断模式*/
//			{
//				ECU_MC33771_CMD((u32)AFE_CELL_DIAG,7);
//				s_u8_BLN_Stp = 1;
//				u8_DelayCount = 0;
//				break;
//			}
//			case 1:			
//			{
//				if(u8_DelayCount > 2)/*大小与周期稳定性有关*/
//				{
//					s_u8_BLN_Stp = 2;
//					u8_DelayCount = 0;
//				}
//				else
//				{
//					u8_DelayCount ++;
//					ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 55,2); 
//				}
//				break;
//			}
//			case 2:			
//			{
//				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 40,5);
//				s_u8_BLN_Stp = 3;
//				break;
//			}
//			case 3:
//			{
//				ECU_MC33771_CCELL_VCT(G_u16_MC3371_AdVlaue);
//				G_u16_Cell_UvOv1[0] =	G_u16_MC3371_AdVlaue[1];
//				G_u16_Cell_UvOv1[1] =	G_u16_MC3371_AdVlaue[2];
//				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 70,8);
//				s_u8_BLN_Stp = 4;
//				break;
//			}
//			case 4:
//			{ 
//				ECU_MC33771_CMD((u32)AFE_CELL_DIAG + 145,5);
//				s_u8_BLN_Stp = 5; 
//				break;
//			}



